package l.l.w.leetcode.CountBinarySubstrings;

/**
 * Created by llw on 2017/10/15.
 */
public class CountBinarySubstrings {

    public int countBinarySubstrings(String s) {
        boolean flag =true;
        char start = s.charAt(0);
        int lenA = 1;
        int lenB =0;
        int result = 0;
        for(int i = 1; i < s.length(); ++i){
            char c = s.charAt(i);
            if(c == start){
                if(flag){
                    lenA++;
                }else{
                    result += Math.min(lenA,lenB);
                    if(start =='0') start ='1';
                    else start='0';
//                    flag = false;
                    lenA = lenB;
                    lenB=1;
                }
            }else{
                lenB++;
                flag = false;
            }
        }
        result += Math.min(lenA,lenB);
        return result;
    }

    public static void main(String[] args) {
        String s = "10101";
        int result = new CountBinarySubstrings().countBinarySubstrings(s);
        System.out.println(result);
    }
}
